package threads;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import data.CustomerInfo;
import rm2.DRSserverinit;
import rm2.UDPDatagram;
import exception.ReturnTooManyException;
import exception.CustomerNotExistsException;
import exception.ItemNotExistsException;

public class CheckItemList// extends Thread
{
	String item;
	CustomerInfo cl;
	String message_stock;
	DRSserverinit drSserverinit;

	public CheckItemList(String item_id, DRSserverinit drSserverinit) {
		this.drSserverinit = drSserverinit;
		this.item = item_id;
		//message_stock = this.drSserverinit.server + " " + "0";
	}

	// public void run()
	public String checkItem(boolean udpCheck) {
		int total = 0;
		//message_stock = drSserverinit.server + " " + "0";
		boolean flagItemFound = false;

		Iterator it = drSserverinit.slist.keySet().iterator();

		try {

			
			while (it.hasNext()) {
				String key = (String) it.next();
				//System.out.println("key: "+key);
				int val = drSserverinit.slist.get(key).getCapacity();
				//System.out.println("value: "+val);

				if (key.equalsIgnoreCase(item)) {

					message_stock =Integer.toString(drSserverinit.slist.get(key)
									.getCapacity());
					total += drSserverinit.slist.get(key).getCapacity();

					 /*System.out.println("\n" + this.item
					 +" --> item Available" + ": " +
					 Integer.toString(drSserverinit.slist.get(key).getCapacity()));*/
					flagItemFound = true;
					break;
				}
			}

			//System.out.println(message_stock);
			if(udpCheck){
			////
			/*message_stock = message_stock
					+ " "
					+ UDPDatagram.messageSendAndReceive("checkstockU~" + item,
							this.drSserverinit.closest_1_server_udp_port,
							this.drSserverinit.closest_1_server_address);
			message_stock = message_stock
					+ " "
					+ UDPDatagram.messageSendAndReceive("checkstockU~" + item,
							this.drSserverinit.closest_2_server_udp_port,
							this.drSserverinit.closest_2_server_address);*/
			total += Integer.parseInt(UDPDatagram.messageSendAndReceive(
					"checkstockU~" + item,
					this.drSserverinit.closest_1_server_udp_port,
					this.drSserverinit.closest_1_server_address));
			total += Integer.parseInt(UDPDatagram.messageSendAndReceive(
					"checkstockU~" + item,
					this.drSserverinit.closest_2_server_udp_port,
					this.drSserverinit.closest_2_server_address));
			}
		
		} catch (Exception ex) {

			System.out.println(ex.getMessage());
			ex.printStackTrace();
		}
		 System.out.println("message stock: "+message_stock);
		 System.out.println("total: "+ total);
		return String.valueOf(total);
	}

	public int maxAvailable(String reply) {
		int max = 0;

		StringTokenizer st = new StringTokenizer(reply, " ");

		while (st.hasMoreTokens()) {
			String token = st.nextToken();

			if (isInteger(token)) {
				if (Integer.parseInt(token) > max)
					max = Integer.parseInt(token);
			}
		}
		return max;
	}

	public boolean isInteger(String input) {
		try {
			Integer.parseInt(input);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

}
